<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>83. 删除排序链表中的重复元素</title>
  </head>
  <body>
    <script>
      //     给定一个已排序的链表的头 head ， 删除所有重复的元素，使每个元素只出现一次 。返回 已排序的链表 。

      // 示例 1：
      // 输入：head = [1,1,2]
      // 输出：[1,2]
      /**
       * Definition for singly-linked list.
       * function ListNode(val, next) {
       *     this.val = (val===undefined ? 0 : val)
       *     this.next = (next===undefined ? null : next)
       * }
       */
      /**
       * @param {ListNode} head
       * @return {ListNode}
       */
      var deleteDuplicates = function (head) {
        if (!head || !head.next) {
          return head
        }
        let isSame = head.val
        let preNode = head
        let compareNode = head.next
        while (compareNode) {
          if (compareNode.val === isSame) {
            preNode.next = compareNode.next
            compareNode = preNode.next
          } else {
            preNode = compareNode
            isSame = compareNode.val
            compareNode = preNode.next
          }
        }
        return head
      }
    </script>
  </body>
</html>
